///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_DBUS_COBJECT_H_
#define _BUOLA_DBUS_COBJECT_H_

#include <buola/dbus/cobjectbase.h>
#include <buola/dbus/cintrospection.h>

namespace buola { namespace dbus {

///\addtogroup dbus
///@{

//! used to construct an object tree instead of a single object
DECLARE_NAME(Tree);

////////////////////////////////////////////////////////////////////////////

/// D-BUS exported object

/// Inherit from this class to make it possible to invoke methods remotely
/// on an object via D-BUS.
///
////////////////////////////////////////////////////////////////////////////

template<typename tSub>
class CObject : public CObjectBase
{
protected:
//! \name constructors and destructor
//@{
    //! register object at path
    CObject(const std::string &pPath,EBusType pType=SESSION)
    {
        RegisterDBusObject(pPath,pType);
    }
    //! create an object tree at path \p pPath instead of a single object
    CObject(NTree,const std::string &pPath,EBusType pType=SESSION)
    {
        RegisterDBusTree(pPath,pType);
    }
    //! don't register object
    CObject()
    {}
    //! destructor
    virtual ~CObject()
    {}
//@}
    
private:
    //! must be reimplemented to process method invocations
    virtual bool DBusCall(CMessage *pM)
    {
        if(!mDBusIntrospection)
        {
            mDBusIntrospection=new CIntrospection<tSub>();
            tSub::Introspect(*mDBusIntrospection);
        }

        const std::string &lMethod=pM->GetMember();

        if(lMethod=="Introspect")
        {
            PMessage lReply=pM->BuildReply();
            std::string lIntrospection=mDBusIntrospection->mXML.ToString();
            append_value(lReply.get(),lIntrospection);
            lReply->Send();

            return true;
        }

        auto lIt=mDBusIntrospection->mMethods.find(lMethod);
                       
        if(lIt!=mDBusIntrospection->mMethods.end())
        {
            lIt->second->Dispatch(pM,dynamic_cast<tSub*>(this));
            return true;
        }

        return false;
    }

    static CIntrospection<tSub> *mDBusIntrospection;
};

template<class tSub>
CIntrospection<tSub> *CObject<tSub>::mDBusIntrospection=nullptr;

///@}

/*namespace dbus*/ } /*namespace buola*/ }

#endif
